using System;
using System.Collections.Generic;
using System.Text;

namespace Wrath.Xml.Objects
{       
    public class ControlTooltip : IElementContainer
    {
        public enum AnchorTo
        {
            None,
            Control,
            Desktop,
            Cursor
        }

        public enum Alignment
        {
            None,
            TopLeft,
            Top,
            TopRight,
            Left,
            Center,
            Right,
            BottomLeft,
            Bottom,
            BottomRight
        }

        private string text;

        public string Text
        {
            get { return text; }
            set { text = value; }
        }	

        private AnchorTo anchor;

        public AnchorTo Anchor
        {
            get { return anchor; }
            set { anchor = value; }
        }

        private Alignment align;

        public Alignment Align
        {
            get { return align; }
            set { align = value; }
        }

        private Position position;

        public Position Position
        {
            get { return position; }
            set { position = value; }
        }

        public ControlTooltip(Element element)
        {
            sourceElement = element;

            Element subElement = element.FindFirstElement("text", false);
            if (subElement != null)
            {
                text = subElement.Value.Substring(0);
            }

            subElement = element.FindFirstElement("anchor", false);
            if (subElement != null)
            {
                if (subElement.Value.ToLower() == "control")
                {
                    anchor = AnchorTo.Control;
                }
                else if (subElement.Value.ToLower() == "desktop")
                {
                    anchor = AnchorTo.Desktop;
                }
                else if (subElement.Value.ToLower() == "cursor")
                {
                    anchor = AnchorTo.Cursor;
                }
                else
                {
                    anchor = AnchorTo.None;
                }
            }

            subElement = element.FindFirstElement("align", false);
            if (subElement != null)
            {
                if (subElement.Value.ToLower() == "topleft")
                {
                    align = Alignment.TopLeft;
                }
                else if (subElement.Value.ToLower() == "top")
                {
                    align = Alignment.Top;
                }
                else if (subElement.Value.ToLower() == "topright")
                {
                    align = Alignment.TopRight;
                }
                else if (subElement.Value.ToLower() == "left")
                {
                    align = Alignment.Left;
                }
                else if (subElement.Value.ToLower() == "center")
                {
                    align = Alignment.Center;
                }
                else if (subElement.Value.ToLower() == "right")
                {
                    align = Alignment.Right;
                }
                else if (subElement.Value.ToLower() == "bottomleft")
                {
                    align = Alignment.BottomLeft;
                }
                else if (subElement.Value.ToLower() == "bottom")
                {
                    align = Alignment.Bottom;
                }
                else if (subElement.Value.ToLower() == "bottomright")
                {
                    align = Alignment.BottomRight;
                }
                else
                {
                    align = Alignment.None;
                }
            }

            subElement = element.FindFirstElement("position", false);
            if (subElement != null)
            {
                position = new Position(subElement);
            }

        }

        #region IElementContainer Members

        private Element sourceElement;
        public Element SourceElement
        {
            get { return sourceElement; }
        }

        public void Commit()
        {
            sourceElement = new Element("tooltip");

            if (!String.IsNullOrEmpty(text))
            {
                Element subElement = new Element("text");
                subElement.Value = text;

                sourceElement.ChildElements.Add(subElement);
            }

            if (anchor != AnchorTo.None)
            {
                Element subElement = new Element("anchor");

                switch (anchor)
                {
                    case AnchorTo.Control: subElement.Value = "control"; break;
                    case AnchorTo.Cursor: subElement.Value = "cursor"; break;
                    case AnchorTo.Desktop: subElement.Value = "desktop"; break;
                }

                sourceElement.ChildElements.Add(subElement);
            }

            if (align != Alignment.None)
            {
                Element subElement = new Element("align");

                switch (align)
                {
                    case Alignment.TopLeft: subElement.Value = "topleft"; break;
                    case Alignment.Top: subElement.Value = "top"; break;
                    case Alignment.TopRight: subElement.Value = "topright"; break;
                    case Alignment.Left: subElement.Value = "left"; break;
                    case Alignment.Center: subElement.Value = "center"; break;
                    case Alignment.Right: subElement.Value = "right"; break;
                    case Alignment.BottomLeft: subElement.Value = "bottomleft"; break;
                    case Alignment.Bottom: subElement.Value = "bottom"; break;
                    case Alignment.BottomRight: subElement.Value = "bottomright"; break;
                }

                sourceElement.ChildElements.Add(subElement);
            }

            if (position != null)
            {
                position.Commit();
                sourceElement.ChildElements.Add(position.SourceElement);
            }
        }

        #endregion
    }
}
